package edu.cebanc.spring.biblioteca.controllers;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.cebanc.spring.biblioteca.bean.AuthorBean;
import edu.cebanc.spring.biblioteca.bean.BookBean;
import edu.cebanc.spring.biblioteca.bean.GenreBean;
import edu.cebanc.spring.biblioteca.services.BooksService;

@Controller
@RequestMapping(value="/books")
public class BookController {
	
	@Autowired
	BooksService booksService;
	BookBean book;
	
	@RequestMapping(method=RequestMethod.GET)
	public String getMainView(Model model) {
		model.addAttribute("book", booksService.listBooks());
		
		return "booksMain";
	}
	
	@RequestMapping(value="/booksForm", method=RequestMethod.GET)
	public String getFormView(Model model) {
		
		BookBean bb = new BookBean();
		model.addAttribute("book", bb);
		
		return "booksForm";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String getUsersData(@Valid BookBean bb, BindingResult result) {
		if(result.hasErrors()) {
			return "redirect:/books/booksForm";
		}
		booksService.saveBook(bb);
		
		return "redirect:/books";
	}
	
	@RequestMapping(value="/bookInfo/{id}", method=RequestMethod.GET)
	public String getInfoView(@PathVariable String id, Model model) {
		BookBean bb = booksService.loadBook(id);
		model.addAttribute("book", bb);
		model.addAttribute("author", bb.getAutores());
		model.addAttribute("genre", bb.getGeneros());
		return "bookInfo";
	}

	@RequestMapping(value="/editBook/{id}", method=RequestMethod.GET)
	public String editUser(@PathVariable String id, Model model) {
		BookBean bb = booksService.loadBook(id);
		model.addAttribute("book", bb);
		model.addAttribute("author", bb.getAutores());
		model.addAttribute("genre", bb.getGeneros());
		return "booksForm";
	}
	
	@RequestMapping(value="/deleteBook/{id}", method=RequestMethod.GET)
	public String delete(@PathVariable String id) {
		booksService.deleteBook(id);
		
		return "redirect:/books";
	}
	
    @RequestMapping(value="addAutorLibro", method=RequestMethod.GET)
    public String getAddAutorView(Model model){
            
            model.addAttribute("autores", booksService.getAuthors(this.book));
            model.addAttribute("autor", new AuthorBean());
            return "librosAutor";
    }
    
    @RequestMapping(value="dataAutorLibro", method=RequestMethod.POST)
    public String dataAutorLibro(AuthorBean autor, BindingResult result, Model model){
            
            if(result.hasErrors()){
                    model.addAttribute("autor", autor);
                    return "librosAutor";
            }
            
            booksService.addAuthor2Book(this.book, autor);
            
            return "redirect:/libros";
    }
    
    @RequestMapping(value="addGeneroLibro", method=RequestMethod.GET)
    public String getAddGeneroView(Model model){
            
            model.addAttribute("generos", booksService.getGenre(this.book));
            model.addAttribute("genero", new GenreBean());
            return "librosGenero";
    }
    
    @RequestMapping(value="dataGeneroLibro", method=RequestMethod.POST)
    public String dataGeneroLibro(GenreBean genero, BindingResult result, Model model){
            if(result.hasErrors()){
                    model.addAttribute("genero", genero);
                    return "librosGenero";
            }
            booksService.addGenre2Book(this.book, genero);
            return "redirect:/libros";
    }
}
